package com.qianbao.qianbaobusiness.ui.invoice;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.GStoreBean;

public class StoreContractListRvAdapter extends BaseRecycleViewAdapter<GStoreBean.ResultBean> {

    private ClickListener mListener;

    public StoreContractListRvAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, final GStoreBean.ResultBean item) {
        final VH vh = (VH) holder;
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.itemClicked(item.getGroupNo(), item.getGroupName());
                }
            }
        });
        vh.mTv.setText(item.getGroupName());
        switch (vh.getItemViewType()) {
            case 1:
                vh.mSeparator.setVisibility(View.VISIBLE);
                break;
            case 2:
                vh.mSeparator.setVisibility(View.GONE);
                break;
            default:
                break;
        }
        vh.mTvAddress.setText(item.getAddress());
    }

    @Override
    protected RecyclerView.ViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_store_contract_list, null);
        return new VH(view);
    }

    @Override
    protected int getBaseItemViewType(int position) {
        int lastIndex = mData.size() - 1;
        if (position == lastIndex) {
            return 2;
        } else {
            return 1;
        }
    }

    class VH extends RecyclerView.ViewHolder {
        TextView mTv;
        TextView mTvAddress;
        View     mSeparator;

        public VH(View itemView) {
            super(itemView);
            mTv = itemView.findViewById(R.id.tv_name);
            mTvAddress = itemView.findViewById(R.id.tv_address);
            mSeparator = itemView.findViewById(R.id.separator);
        }
    }

    public interface ClickListener {
        void itemClicked(String merchantNo, String merchantName);
    }

    public void setClickListener(ClickListener listener) {
        mListener = listener;
    }
}
